#include <iostream>
using namespace std;
struct nod {
    int info;
    struct nod *next,*prev;
};
typedef struct nod Tnod;
typedef Tnod *ref;

class ld
{
    ref nod_prim;
public:
    ld();
    ~ld();
    void Insd_P();
    void Insd_P(int);
    void Insd_Cf();
    void readList();
    void print();
    ld operator+(Tnod);
    ld operator+(int);
    ld operator+(ld);
    bool operator==(ld);
    bool operator!=(ld);
    ld reuniune(ld);
    void intersectie(ld);
    void sort();
};

ld::ld()
{
    
}

ld::~ld(){}

void ld::Insd_P()
{
    ref q;
    q=new Tnod;
    cout<<"Info:";
    cin>>q->info;
    q->next=NULL;
    q->prev=NULL;
    nod_prim=q;
}

void ld::Insd_P(int x)
{
    ref q;
    q=new Tnod;
    q->info=x;
    q->next=NULL;
    q->prev=NULL;
    nod_prim=q;
}

void ld::Insd_Cf(void)
{
    ref q;
    q=new Tnod;
    cout<<"Info:";
    cin>>q->info;
    q->next=nod_prim;
    nod_prim->prev=q;
    nod_prim = q;
}
void ld::readList()
{
    Insd_P();
    char c;
    cout<<"Add? y/n";
    cin>>c;
    while (c=='y')
    {
        Insd_Cf();
        cout<<"Add? y/n";
        cin>>c;
    }
}
void ld::print()
{
    ref r;
    r = nod_prim;
    while(r != NULL)
    {
        cout<<"Info = "<<r->info<<endl;
        r = r->next;
    }
    cout<<"End of List";
}
bool ld::operator==(ld list)
{
    ref r=this->nod_prim, q=list.nod_prim;
    while (r!=NULL && q!=NULL)
    {
        if (r->info != q->info)
            return  false;
        else r=r->next,q=q->next;
    }
    if (r==NULL && q==NULL)
        return true;
    else return false;
}

bool ld::operator!=(ld list)
{
    ref r=this->nod_prim, q=list.nod_prim;
    while (r!=NULL && q!=NULL)
    {
        if (r->info != q->info)
            return  true;
        else r=r->next,q=q->next;
    }
    if (r==NULL && q==NULL)
        return false;
    else return true;
}

ld ld::operator+(Tnod n)
{
    ref q;
    q=new Tnod;
    q->info=n.info;
    q->next=nod_prim;
    nod_prim->prev=q;
    nod_prim=q;
    return *this;
}
ld ld::operator+(int x)
{
    ref q;
    q=new Tnod;
    q->info=x;
    q->next=nod_prim;
    nod_prim->prev=q;
    nod_prim=q;
    return *this;
}
ld ld::operator+(ld list)
{
    ref r=this->nod_prim, q=list.nod_prim;
    while (r->next!=NULL)
        r=r->next;
    r->next=q;
    q->prev=r;
    return *this;
}
void ld::sort()
{
    ref r=nod_prim;
    ref q=nod_prim;
    while (r!=NULL)
    {
        while (q!=NULL)
        {
            if (r->info < q->info)
            {
                int temp=r->info;
                r->info=q->info;
                q->info=temp;
            }
            q=q->next;
        }
        q=this->nod_prim;
        r=r->next;
    }
}

ld ld::reuniune(ld list)
{
    ld result;
    result=*this + list;
    result.sort();
    ref r,q;
    r=nod_prim;
    q=nod_prim;
    while (r->next!=NULL)
    {
        if (r->info == r->next->info)
        {
            q=r->next;
            delete r;
            r->prev->next=q;
            q->prev=r->prev;
            r=q;
        }
        r=r->next;
    }
    return result;
}

void ld::intersectie(ld list)
{
    ld result,temp;
    bool created=false;
    temp=*this + list;
    temp.sort();
    ref r,q;
    r=nod_prim;
    q=nod_prim;
    while (r->next!=NULL)
    {
        if (r->info == r->next->info)
        {
            if (!created)
            {
                result.Insd_P(r->info);
                created=true;
            }
            else {
                result = result + *r;
            }
        }
        r=r->next;
    }
    this->nod_prim=result.nod_prim;
}

int main()
{
    ld list1,list2,list3;
    list1.readList();
    list2.readList();
    list1.intersectie(list2);
    cout<<endl<<endl;
    list1.print();
}
